package com.os.sys;

import com.os.sys.config.MyProps;
import com.os.sys.controller.filter.AuthFilter;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.CrossOrigin;
import util.IdWorker;
import util.JwtUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/12/3.
 */
@ComponentScan(basePackages = {"com.os","util"} )
@MapperScan("com.os.**.mapper") //扫描的mapper
@SpringBootApplication
//@EnableEurekaClient
@EnableTransactionManagement //启用事务配置管理--见TxAnoConfig
@EnableAsync //开启异步处理
@EnableScheduling //支持定时任务
@EnableCaching //启动spring缓存
@EnableConfigurationProperties
@CrossOrigin
public class SysApplication extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SysApplication.class);
    }
    public static void main(String[] args) {
        SpringApplication.run(SysApplication.class, args);
        AnnotationConfigApplicationContext context =
                new AnnotationConfigApplicationContext(MyProps.class);
        MyProps service = context.getBean(MyProps.class);
        System.out.println("service.path_base::"+ service.path_base);
    }

    /**
     * 从别的容器中（tensquare_common）引入IdWorker类
     */
    @Bean
    public IdWorker idWorker(){
        return new IdWorker(1,1);
    }
    @Bean
    public BCryptPasswordEncoder encoder(){
        return new BCryptPasswordEncoder();
    }

    @Lazy
    @Bean
    public JwtUtil jwtUtil(){
        return new JwtUtil();
    }

    //注册filter
    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        //JwtUtil f = new JwtUtil();
        //f.parseJWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlNDNiNGUyNGZiODZmNDFiMGFmZmVhOCIsImlhdCI6MTU4MTQ5NTUyMiwiZXhwIjoxNTgyNzA1MTIyfQ.XHijvYj3hvdpAkPL9RrbMI3CfE23qVa-_hqExMEbtEo");

        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        AuthFilter tokenAuthorFilter = new AuthFilter();

        registrationBean.setFilter(tokenAuthorFilter);
        List<String> urlPatterns = new ArrayList<String>();
        //urlPatterns.add("/api/*");
        //对以下目录执行权限验证  这里也可以设置为/** ,然后在AuthFilter中来作白名单控制（除白名单的不验证，其它的路径默认都验证）
        urlPatterns.add("/role1/*");
        registrationBean.setUrlPatterns(urlPatterns);
        return registrationBean;
    }

    //@Autowired
    //RedisConnectionFactory redisConnectionFactory;

//    @Bean
//    //@ConditionalOnBean(value = {StandaloneRedisConfig.class})
//    public RedisConnectionFactory standaloneRedisConnectionFactory() {
//        JedisConnectionFactory factory = new JedisConnectionFactory(new RedisStandaloneConfiguration("localhost", 6379));
//        return factory;
//    }
}

